package com.qfedu.yanglao20admin.article.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.article.Article;
import com.qfedu.yanglao20admin.article.dao.ArticleDao;
import com.qfedu.yanglao20admin.article.param.ArticleParam;
import com.qfedu.yanglao20admin.article.service.ArticleService;
import com.qfedu.yanglao20admin.article.vo.ArticleVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ArticleServiceImpl implements ArticleService {


    @Resource
    private ArticleDao articleDao;

    @Override
    public Page<ArticleVO> pageArticle(ArticleParam param) {

        Page<ArticleVO> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<ArticleVO> articleVOS = articleDao.selectList(param);


        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        page.setRecords(articleVOS);

        Long aLong = articleDao.selectCount(null);
        page.setTotal(aLong);

        return page;
    }

    @Override
    public void deleteById(Integer id) {
        if (id > 5) {
            articleDao.deleteById(id);
        } else {
            throw new CommonException(CommonConstants.ERROR, "该id是主类id不能删除");
        }
    }

    @Override
    public void addArticle(Article article) {
        if (article.getArticleTitle() == null || article.getArticleTitle().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请写文章标题名称");
        }

        Article articleTitle = articleDao.selectByArticleTitle(article.getArticleTitle());
        if (articleTitle != null) {
            throw new CommonException(CommonConstants.ERROR, "该文章标题名称已存在");
        }

        Article articles = articleDao.selectTypeIdByArticleId(article.getArticleType());
        Integer typeId = articles.getTypeId();

        String articleTitle1 = article.getArticleTitle();
        String articleType = article.getArticleType();
        String articleCover = article.getArticleCover();
        String articleSource = article.getArticleSource();
        String articleContent = article.getArticleContent();
        Integer articleSort = article.getArticleSort();
        Integer articleInfo = article.getArticleInfo();

        //把相对应的数据()装到一个新实体类对象中
        Article article2 = new Article();
        article2.setTypeId(typeId);
        article2.setArticleTitle(articleTitle1);
        article2.setArticleType(articleType);
        article2.setArticleCover(articleCover);
        article2.setArticleSource(articleSource);
        article2.setArticleContent(articleContent);
        article2.setArticleSort(articleSort);
        article2.setArticleInfo(articleInfo);

        articleDao.insertArticle(article2);

    }

    @Override
    public Article queryArticle(Integer id) {
        return articleDao.selectByArticleId(id);
    }

    @Override
    public void updateArticle(Article article) {
        if (article.getArticleTitle() == null || article.getArticleTitle().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填文章标题名称");
        }


        Article articleTitle = articleDao.selectByArticleTitle(article.getArticleTitle());
        if (articleTitle != null) {
            if (!articleTitle.getId().equals(article.getId())) {
                throw new CommonException(CommonConstants.ERROR, "该文章标题名称已存在");
            }

        }

        articleDao.updateById(article);
    }


    @Override
    public Article selectByArticleId(Integer id) {
        return articleDao.selectByArticleId(id);
    }
}
